package com.car.rental.vehicle.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.NonNull;
import java.io.File;

@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Value("${upload.base-path}")
    private String basePath;
    
    @Value("${upload.path}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(@NonNull ResourceHandlerRegistry registry) {
        String location = "file:" + basePath + uploadPath;
        File uploadDir = new File(basePath + uploadPath);
        log.info("实际文件存储路径: {}", uploadDir.getAbsolutePath());
        
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
            log.info("创建上传目录: {}", uploadDir.getAbsolutePath());
        }
        
        registry.addResourceHandler("/vehicle/uploads/vehicle/**")
                .addResourceLocations(location)
                .setCachePeriod(3600)  // 设置缓存时间
                .resourceChain(true);  // 开启资源链优化
        
        log.info("静态资源映射配置 - 访问路径: /vehicle/uploads/vehicle/**, 实际路径: {}", location);
    }
} 